package com.villaday.idf.activity;

import android.content.Intent;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.adapter3.TixianJiluAdapter;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.base.OnAdapterCallbackListener;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.custom.RVItemDecoration;
import com.villaday.idf.custom.pop.ShuomingTixianJiLuPop;
import com.villaday.idf.custom.pop.TiXianHistoryPop;
import com.villaday.idf.model.memberProfitsEntity;
import com.villaday.idf.model3.Vips_brokerage_recordsEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.ToastUtil;
import com.villaday.idf.util.ToolBarUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/*
 *
 *我的余额
 * */
public class MyBalanceActivity extends BaseActivity implements OkHttpUtil.OnDataListener, OnAdapterCallbackListener, TiXianHistoryPop.popuClick {


    @BindView(R.id.toolbar_text)
    Toolbar toolbarText;
    @BindView(R.id.tv_daijiedong)
    TextView tvDaijiedong;
    @BindView(R.id.tv_blanceNum)
    TextView tvBlanceNum;
    @BindView(R.id.tv_ljnum)
    TextView tvLjnum;
    @BindView(R.id.tv_lbnum)
    TextView tvLbnum;
    @BindView(R.id.tv_tixianJilu)
    TextView tvTixianJilu;
    @BindView(R.id.rv)
    RecyclerView rv;
    private Gson gson = new GsonBuilder().create();
    private com.villaday.idf.model.memberProfitsEntity memberProfitsEntity;
    private LoadingDialog loadingDialog;
    List<Vips_brokerage_recordsEntity.BodyBean.ListBean> data = new ArrayList();

    private int pageNo = 1;
    private int nextIndex = 1;
    private int pageSize = 3;
    private TixianJiluAdapter tixianJiluAdapter;
    private PopupWindow popupWindow;
    private int sortType;

    @Override
    protected int getViewResId() {
        return R.layout.activity_my_balance;
    }

    @Override
    protected void init() {
        ToolBarUtil.initToolBar(toolbarText, "我的余额", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        }, "说明", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShuomingTixianJiLuPop shuomingTixianJiLuPop = new ShuomingTixianJiLuPop(MyBalanceActivity.this);
                shuomingTixianJiLuPop.showPopupWindow();

            }
        });

        loadingDialog = LoadingDialog.newInstance("加载中...");
        loadingDialog.show(getFragmentManager());

        rv.setLayoutManager(new LinearLayoutManager(this));
        tixianJiluAdapter = new TixianJiluAdapter(this, this);

        rv.setAdapter(tixianJiluAdapter);

        rv.addItemDecoration(new RVItemDecoration(LinearLayout.VERTICAL, PhoneUtil.dp2px(this, 1), getResources().getColor(R.color.colorBgEEE), PhoneUtil.dp2px(this, 1)));
    }

    @Override
    protected void loadData() {
        Map<String, String> map = new HashMap<>();
        OkHttpUtil.getJSON(this, Constant.URL.member_profits, map, this);
    }

    @OnClick({R.id.tv_tixian, R.id.tv_tixianJilu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_tixian:
                startActivity(new Intent(MyBalanceActivity.this, CashWithdrawalActivity.class));
                onBackPressed();
                break;
            case R.id.tv_tixianJilu:
                if (memberProfitsEntity != null && memberProfitsEntity.getBody() != null && memberProfitsEntity.getBody().getArokerageRecordParams() != null && memberProfitsEntity.getBody().getArokerageRecordParams().size() > 0) {
                    TiXianHistoryPop tiXianHistoryPop = new TiXianHistoryPop(this, memberProfitsEntity.getBody().getArokerageRecordParams(), this);
                    tiXianHistoryPop.setBackground(0);
                    tiXianHistoryPop.showPopupWindow(tvTixianJilu);
                }


                break;
        }
    }

    public void onResponse(String url, String json) {
        if (!TextUtils.isEmpty(json)) {
            if (Constant.URL.member_profits.equals(url)) { //收益统计
                dismissLoading();
                LogeUtil.e("member_profits", json);
                memberProfitsEntity = gson.fromJson(json, memberProfitsEntity.class);
                if (Constant.Strings.SUC.equals(memberProfitsEntity.getCode())) {
                    UpdateData(memberProfitsEntity.getBody());
                } else {
                    ToastUtil.initToast(this, memberProfitsEntity.getMsg());
                }
            } else if (Constant.URL.vips_brokerage_records.equals(url)) {
                dismissLoading();
                LogeUtil.e("vips_brokerage_records", json);
                Vips_brokerage_recordsEntity vips_brokerage_recordsEntity = gson.fromJson(json, Vips_brokerage_recordsEntity.class);
                removeLoadingItem();
                if (pageNo == 1) {
                    data.clear();
                }

                if ("200".equals(vips_brokerage_recordsEntity.getCode())) {
                   /* if (pageNo == 1 && presellEntity.getBody().getRecomendList() != null && presellEntity.getBody().getRecomendList().getList() != null) {

                        dataHead.addAll(presellEntity.getBody().getRecomendList().getList());
                        homeListSaleHeadAdapter.setData(dataHead);
                    }*/
                    boolean isTranslucentStatus = false;

                    data.addAll(vips_brokerage_recordsEntity.getBody().getList());

                    if (vips_brokerage_recordsEntity.getBody().isHasNext()) {
                        data.add(new Vips_brokerage_recordsEntity.BodyBean.ListBean(1));
                        nextIndex = pageNo + 1;

                    } else {
                        addBaseLine();
                    }

                } else {
                    addBaseLine();
                }
                tixianJiluAdapter.setData(data);
            }
        }
    }

    private void removeLoadingItem() {
        if (data.size() > 0) {
            if (data.get(data.size() - 1).getMyType() == 1) {
                data.remove(data.size() - 1);
            }
        }
    }

    private void addBaseLine() {
        if (pageNo != 1) {
            data.add(new Vips_brokerage_recordsEntity.BodyBean.ListBean(2));
        }
    }

    private void UpdateData(memberProfitsEntity.BodyBean body) {
        tvDaijiedong.setText(body.getFrozenDescription());
        tvBlanceNum.setText("" + body.getBalanceValue());
        tvLjnum.setText("" + body.getTotalProfit().getAmountsValue());
        tvLbnum.setText("" + body.getFissionProfit().getAmountsValue());
        if (body.getArokerageRecordParams() != null && body.getArokerageRecordParams().size() > 0) {
            tvTixianJilu.setText(body.getArokerageRecordParams().get(0).getName());
            List<com.villaday.idf.model.memberProfitsEntity.BodyBean.BrokerageRecordParams> arokerageRecordParams = memberProfitsEntity.getBody().getArokerageRecordParams();
            sortType = arokerageRecordParams.get(0).getParam();
            getHistoryData();
        }

    }

    private void getHistoryData() {
        Map<String, String> map = new HashMap<>();

        map.put("sortType", sortType + "");

        map.put("pageNo", pageNo + "");
        map.put("pageSize", pageSize + "");
        OkHttpUtil.getJSON(this, Constant.URL.vips_brokerage_records, map, this);
    }

    @Override
    public void onFailure(String url, String error) {

    }

    private void dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    @Override
    public void onCallback() {
        if (nextIndex == pageNo + 1) {
            pageNo++;
            getHistoryData();
        }
    }

    @Override
    public void popuClick(int position) {
        tvTixianJilu.setText(memberProfitsEntity.getBody().getArokerageRecordParams().get(position).getName());
        sortType = memberProfitsEntity.getBody().getArokerageRecordParams().get(position).getParam();
        pageNo = 1;
        nextIndex = 1;
        getHistoryData();
    }
}
